package me.luger.project.picc.controller.api;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.project.picc.entity.ActivityEnum;
import me.luger.project.picc.entity.PrizeList;
import me.luger.project.picc.entity.picc.PolicyResult;
import me.luger.project.picc.service.PrizeListService;
import me.luger.project.picc.service.picc.PiccService;
import me.luger.project.picc.service.picc.TongjiService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;

import me.luger.project.picc.service.SatisfyQuestionService;
import me.luger.project.picc.entity.SatisfyQuestion;

import java.util.Map;

/**
 * 用户满意度有奖调查答卷列表 controller
 */
@Controller
@RequestMapping("/api/picc/satisfyQuestion")
public class SatisfyQuestionApiController extends BaseController {

    @Autowired
    private SatisfyQuestionService satisfyQuestionService;

    @Autowired
    private PrizeListService prizeListService;

    @Autowired
    private PiccService piccService;

    @Autowired
    private TongjiService tongjiService;



    @RequestMapping(value = "/save",name = "客户满意度有奖调查保存接口")
    @ResponseBody
    public Object save(SatisfyQuestion satisfyQuestion){


        try{
            String pid = satisfyQuestion.getPid();
            String openId = satisfyQuestion.getOpenId();
            String userName = satisfyQuestion.getUserName();

            boolean exist = false,normal = false;
            if(StringUtil.isNotBlank(pid)){
                PolicyResult policyResult = piccService.checkPolicyResult(openId,userName,pid);
                exist = policyResult.isExist();//保单是否存在
                normal = policyResult.isNormal();//是否有效（在有效期内）
            }


            if(!exist || !normal || StringUtil.isBlank(openId)){
                satisfyQuestion.setDoPrizeStatus(0);
            }else{
                satisfyQuestion.setDoPrizeStatus(1);
            }

//            if(!exist){
//                return Result.info(-1,"保单号不存在");
//            }
//
//            if(!normal){
//                return Result.info(-1,"保单已失效");
//            }


            satisfyQuestion = satisfyQuestionService.insert(satisfyQuestion);
            tongjiService.addUserCount(openId, ActivityEnum.SATISFY_QUESTION,0);
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("bizId",satisfyQuestion.getId());
    }

    @RequestMapping(value = "/status",name = "用户是否参加客户满意度有奖调查抽奖")
    @ResponseBody
    public Object status(String openId){
        Map<String,Object> result = new HashedMap();
        boolean hasSubmitQuestion = false,hasDoDraw=false,hasGetPrize=false,hasCompleteAdress=false;
        int bizId = 0;
        String prizeSecrect = "";
        PrizeList prizeList = null;
        try{
            SatisfyQuestion satisfyQuestion = satisfyQuestionService.fetchByOpenId(openId);
            if(null != satisfyQuestion){
                bizId = satisfyQuestion.getId();
                hasSubmitQuestion=true;

                prizeList = prizeListService.fetchByOpenIdAndTypeAndBizId(openId, ActivityEnum.SATISFY_QUESTION.getActivityId(),bizId);

                //用户是否抽奖
                if(null != prizeList){
                    hasDoDraw = true;
                }

                //如果用户抽过奖返回用户是否中奖。
                if(hasDoDraw && prizeList.getPrizeResult() == 1){
                    hasGetPrize = true;
                }

                if(hasGetPrize && StringUtil.isNotBlank(prizeList.getAddress()) && StringUtil.isNotBlank(prizeList.getZcode())){
                    hasCompleteAdress = true;
                }

            }


            if(null != prizeList && hasGetPrize && !hasCompleteAdress){
                prizeSecrect = prizeList.getPrizeSecrect();
            }

            result.put("hasSubmitQuestion",hasSubmitQuestion);
            result.put("hasDoDraw",hasDoDraw);
            result.put("hasGetPrize",hasGetPrize);
            result.put("hasCompleteAdress",hasCompleteAdress);
            result.put("bizId",bizId);
            result.put("prizeSecrect",prizeSecrect);

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success(result);
    }

    @RequestMapping(value = "",name = "接口描述")
    @ResponseBody
    public Object demo(){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }
}
